package com.meilai.project.util;

import com.meilai.project.util.interf.OssUtilInterface;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.InputStream;

/**
 * @author 张驰
 * @description
 * @date 2022-09-21 09:21
 */
@Component
@Log4j2
public class OssUtil implements OssUtilInterface {

    @Value("#{${oss-use}}")
    private int useOss;
    @Autowired
    private OssAliyunUtil ossAliyunUtil;
    @Autowired
    private ObsCtyunUtil obsCtyunUtil;

    private OssUtilInterface getUtil() {
        OssUtilInterface result = null;
        switch (useOss) {
            case 1:
                result = ossAliyunUtil;
                break;
            case 2:
                result = obsCtyunUtil;
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + useOss);
        }
        return result;
    }

    @Override
    public void store(InputStream inputStream, long contentLength, String contentType, String keyName) {
        getUtil().store(inputStream, contentLength, contentType, keyName);
    }

    @Override
    public void storeSimple(InputStream inputStream, String keyName) {
        getUtil().storeSimple(inputStream, keyName);
    }

    @Override
    public void delete(String keyName) {
        getUtil().delete(keyName);
    }

    @Override
    public String generateUrl(String keyName) {
        return getUtil().generateUrl(keyName);
    }
}
